#!/bin/bash

echo "========================================================"
echo "最终验收测试"
echo "========================================================"
echo ""

# 测试计数器
total_tests=0
passed_tests=0
failed_tests=0

# 测试函数
test_case() {
    local test_name="$1"
    local command="$2"
    local expected_in_output="$3"
    
    total_tests=$((total_tests + 1))
    echo "测试 $total_tests: $test_name"
    
    output=$($command 2>&1)
    
    if echo "$output" | grep -q "$expected_in_output"; then
        echo "  ✓ 通过"
        passed_tests=$((passed_tests + 1))
    else
        echo "  ✗ 失败"
        echo "  期望包含: $expected_in_output"
        echo "  实际输出: $output"
        failed_tests=$((failed_tests + 1))
    fi
    echo ""
}

# 执行测试
test_case "顶层到子模块连接 - clk信号" \
    "./moparse -i test_connection.modb -m top_module --show-connect-top2sub" \
    "u_sub_a.clk"

test_case "顶层到子模块连接 - 多端口连接" \
    "./moparse -i test_connection.modb -m top_module --show-connect-top2sub" \
    "|----"

test_case "过滤特定子模块" \
    "./moparse -i test_connection.modb -m top_module --show-connect-top2sub -s u_sub_a" \
    "unconnected"

test_case "子模块间连接 - 显示方向" \
    "./moparse -i test_connection.modb -m top_module --show-connect-sub2sub -sa u_sub_a -sb u_sub_b" \
    "input"

test_case "子模块间连接 - 显示位宽" \
    "./moparse -i test_connection.modb -m top_module --show-connect-sub2sub -sa u_sub_a -sb u_sub_b" \
    "\[7:0\]"

test_case "流水线测试 - 三个子模块" \
    "./moparse -i test_pipeline.modb -m top_module --show-connect-top2sub" \
    "u_sub_c"

test_case "子模块间连接 - A到B" \
    "./moparse -i test_pipeline.modb -m top_module --show-connect-sub2sub -sa u_sub_a -sb u_sub_b" \
    "data_out"

test_case "子模块间连接 - B到C" \
    "./moparse -i test_pipeline.modb -m top_module --show-connect-sub2sub -sa u_sub_b -sb u_sub_c" \
    "data_in"

# 显示测试结果
echo "========================================================"
echo "测试结果总结"
echo "========================================================"
echo "总测试数: $total_tests"
echo "通过: $passed_tests"
echo "失败: $failed_tests"
echo ""

if [ $failed_tests -eq 0 ]; then
    echo "🎉 所有测试通过！"
    exit 0
else
    echo "❌ 有测试失败，请检查！"
    exit 1
fi
